from enum import Enum
from .board import Board

# COLOR = {"BLACK":1,"WHITE":2}
# ID = {"PAWN":1,"ROOK":2,"KNIGHT":3,"BISHOP":4,"QUEEN":5,"KING":6}

class COLOR(Enum):
    BLACK = 1
    WHITE = 2

class PIECE_ID(Enum):
    PAWN = 1
    ROOK = 2
    KNIGHT = 3
    BISHOP = 4
    QUEEN = 5
    KING = 6

class PieceRender():
    def __init__(self):
        pass

class Piece(PieceRender):
    def __init__(self,color:int,x:int,y:int):
        super().__init__()
        self.color = color
        self.x = x
        self.y = y

        self.board = Board()

    def get_movable(self):
        return []

class Pawn(Piece):
    ID = PIECE_ID.PAWN

    def __init__(self, color: int, x: int, y: int):
        super().__init__(color, x, y)

    def get_movable(self):
        returnlist = []
        if self.color == COLOR["WHITE"]:
            if self.board.board[self.y + 1][self.x].COLOR == COLOR["NULLCOLOR"]:
                returnlist.append([self.x,self.y + 1])
                if not self.Moved and self.y == 1 and self.board.board[self.y + 2][self.x].COLOR == COLOR["NULLCOLOR"]:
                    #开局第一步
                    returnlist.append([self.x,self.y+2])
            if self.board.board[self.x + 1][self.y + 1].COLOR != COLOR["NULLCOLOR"] and self.board.board[self.x - 1][self.y + 1].COLOR == COLOR["BLACK"]:
                returnlist.append([self.x + 1,self.y + 1])
            if self.board.board[self.x - 1][self.y + 1].COLOR != COLOR["NULLCOLOR"] and self.board.board[self.x - 1][self.y + 1].COLOR == COLOR["BLACK"]:
                returnlist.append([self.x-1,self.y+1])
                
        else :
            if self.board.board[self.y - 1][self.x].COLOR == COLOR["NULLCOLOR"]:
                returnlist.append([self.x,self.y - 1])
                if not self.Moved and self.y == 6:
                        #开局第一步
                    returnlist.append([self.x,self.y - 2])
            if self.board.board[self.x + 1][self.y - 1].COLOR != COLOR["NULLCOLOR"] and self.board.board[self.x - 1][self.y + 1].COLOR == COLOR["WHITE"]:
                returnlist.append([self.x + 1,self.y - 1])
            if self.board.board[self.x - 1][self.y - 1].COLOR != COLOR["NULLCOLOR"] and self.board.board[self.x - 1][self.y + 1].COLOR == COLOR["WHITE"]:
                returnlist.append([self.x + 1,self.y - 1])
        self.Moved = True
        return returnlist

class Rook(Piece):
    ID =PIECE_ID.ROOK

    def __init__(self, color, x, y):
        super().__init__(color, x, y)
    
    def get_movable(self):
        pos_list = []
        for x in reversed(range(0, self.x)):
            if self.board.is_null_piece(x,self.y):
                pos_list.append((x, self.y))
            elif self.board.get_piece(x, self.y).color != self.color:
                pos_list.append((x, self.y))
                break
            elif self.board.get_piece(x, self.y).color == self.color:
                break
        
        for x in range(self.x+1, self.board.width):
            if self.board.is_null_piece(x,self.y):
                pos_list.append((x, self.y))
            elif self.board.get_piece(x, self.y).color != self.color:
                pos_list.append((x, self.y))
                break
            elif self.board.get_piece(x, self.y).color == self.color:
                break

        for y in reversed(range(0, self.y)):
            if self.board.is_null_piece(self.x, y):
                pos_list.append((self.x, y))
            elif self.board.get_piece(self.x, y).color != self.color:
                pos_list.append((self.x, y))
                break
            elif self.board.get_piece(x, self.y).color == self.color:
                break

        for y in range(self.y+1, self.board.height):
            if self.board.is_null_piece(self.x, y):
                pos_list.append((self.x, y))
            elif self.board.get_piece(self.x, y).color != self.color:
                pos_list.append((self.x, y))
                break
            elif self.board.get_piece(x, self.y).color == self.color:
                break
        return pos_list

class Bishop(Piece):
    ID = PIECE_ID.BISHOP

    def get_movable(self):
        returnlist = []
        A1 = False
        A2 = False
        B1 = False
        B2 = False
        for i in range(1,max(self.x - 1,7 - self.x,self.y - 1,7 - self.y)):
            if not A1 and self.x + i <= 7: #边界判断
                if self.board.board[self.x + i][self.y + i].COLOR != COLOR["NULLCOLOR"]: #如果移动路径上有棋子
                    if self.board.board[self.x + i][self.y + i].COLOR != self.color: #如果是敌方棋子
                        returnlist.append([self.x + i,self.y + i]) #吃子
                    A1 = True #检索停止
                else: #空气
                    returnlist.append([self.x + i,self.y + i])
            elif not A1:
                A1 = True
            if not A2 and self.x - i >= 0:
                if self.board.board[self.x - i][self.y - i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x - i][self.y - i].COLOR != self.color:
                        returnlist.append([self.x - i,self.y - i])
                    A2 = True
                else:
                    returnlist.append([self.x - i,self.y - i])
            elif not A2:
                A2 = True
            if not B1 and self.y + i <= 7:
                if self.board.board[self.x - i][self.y + i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x - i][self.y + i].COLOR != self.color:
                        returnlist.append([self.x - i, self.y + i])
                    B1 = True
                else:
                    returnlist.append([self.x - i,self.y + i])
            elif not B1:
                B1 = True
            if not B2 and self.y - i >= 0:
                if self.board.board[self.x + i][self.y - i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x + i][self.y - i].COLOR != self.color:
                        returnlist.append([self.x + i,self.y - i])
                    B2 = True
                else:
                    returnlist.append([self.x + i,self.y - i])
            elif not B2:
                B2 = True
        return returnlist

class Queen(Piece):
    ID = PIECE_ID.QUEEN

    def get_movable(self):
        returnlist = []
        X1 = False
        X2 = False
        Y1 = False
        Y2 = False
        A1 = False
        A2 = False
        B1 = False
        B2 = False
        for i in range(1,max(self.x - 1,7 - self.x,self.y - 1,7 - self.y)):
            if not X1 and self.x + i <= 7: #边界判断
                if self.board.board[self.x + i][self.y].COLOR != COLOR["NULLCOLOR"]: #如果移动路径上有棋子
                    if self.board.board[self.x + i][self.y].COLOR != self.color: #如果是敌方棋子
                        returnlist.append([self.x + i,self.y]) #吃子
                    X1 = True #检索停止
                else: #空气
                    returnlist.append([self.x + i,self.y])
            elif not X1:
                X1 = True
            if not X2 and self.x - i >= 0:
                if self.board.board[self.x - i][self.y].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x - i][self.y].COLOR != self.color:
                        returnlist.append([self.x - i,self.y])
                    X2 = True
                else:
                    returnlist.append([self.x - i,self.y])
            elif not X2:
                X2 = True
            if not Y1 and self.y + i <= 7:
                if self.board.board[self.x][self.y + i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x][self.y + i].COLOR != self.color:
                        returnlist.append([self.x, self.y + i])
                    Y1 = True
                else:
                    returnlist.append([self.x,self.y + i])
            elif not Y1:
                Y1 = True
            if not Y2 and self.y - i >= 0:
                if self.board.board[self.x][self.y - i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x][self.y - i].COLOR != self.color:
                        returnlist.append([self.x,self.y - i])
                    Y2 = True
                else:
                    returnlist.append([self.x,self.y - i])
            elif not Y2:
                Y2 = True
            if not A1 and self.x + i <= 7: #边界判断
                if self.board.board[self.x + i][self.y + i].COLOR != COLOR["NULLCOLOR"]: #如果移动路径上有棋子
                    if self.board.board[self.x + i][self.y + i].COLOR != self.color: #如果是敌方棋子
                        returnlist.append([self.x + i,self.y + i]) #吃子
                    A1 = True #检索停止
                else: #空气
                    returnlist.append([self.x + i,self.y + i])
            elif not A1:
                A1 = True
            if not A2 and self.x - i >= 0:
                if self.board.board[self.x - i][self.y - i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x - i][self.y - i].COLOR != self.color:
                        returnlist.append([self.x - i,self.y - i])
                    A2 = True
                else:
                    returnlist.append([self.x - i,self.y - i])
            elif not A2:
                A2 = True
            if not B1 and self.y + i <= 7:
                if self.board.board[self.x - i][self.y + i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x - i][self.y + i].COLOR != self.color:
                        returnlist.append([self.x - i, self.y + i])
                    B1 = True
                else:
                    returnlist.append([self.x - i,self.y + i])
            elif not B1:
                B1 = True
            if not B2 and self.y - i >= 0:
                if self.board.board[self.x + i][self.y - i].COLOR != COLOR["NULLCOLOR"]:
                    if self.board.board[self.x + i][self.y - i].COLOR != self.color:
                        returnlist.append([self.x + i,self.y - i])
                    B2 = True
                else:
                    returnlist.append([self.x + i,self.y - i])
            elif not B2:
                B2 = True
        return returnlist

class Knight(Piece):
    ID = PIECE_ID.KNIGHT

    def get_movable(self):
        prelist = [[1,2],[1,-2],[2,1],[2,-1],[-1,2],[-1,-2],[-2,1],[-2,-1]]
        returnlist = []
        x = 0
        y = 0
        for i in prelist:
            x = self.x + i[0]
            y = self.y + i[1]
            if x > 7 or y > 7 or x < 0 or y < 0:
                continue
            elif self.board.board[x][y].COLOR != self.color:
                returnlist.append([x,y])
        return returnlist

class King(Piece):
    ID = PIECE_ID.KING
    
    def get_movable(self):
        returnlist = []
        prelist = [[1,0],[1,1],[1,-1],[0,1],[0,-1],[-1,0],[-1,1],[-1,-1]]
        x = 0
        y = 0
        for i in prelist:
            x = self.x + i[0]
            y = self.y + i[1]
            if x > 7 or y > 7 or x < 0 or y < 0:
                continue
            elif self.board.board[x][y].COLOR != self.color:
                returnlist.append([x,y])
        return returnlist
